%%%-----------------------------------------------------------------------------
%%% @doc
%%% web phone ets
%%%
%%% @end
%%%-----------------------------------------------------------------------------
-module(token_check_interval).

-behavior(gen_server).

-include("emqttd.hrl").

-export([start_link/0]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-export([clear_timer_interval/0]).

-record(state, {}).

-define(TABLE, ?MODULE).

%%------------------------------------------------------------------------------
%% @doc
%% Start web phone  ets.
%%
%% @end
%%------------------------------------------------------------------------------
start_link() ->
  gen_server2:start_link({local, ?MODULE}, ?MODULE, [], []).

%%%=============================================================================
%%% gen_server callbacks
%%%=============================================================================

init([]) ->
  process_flag(trap_exit, true),
  ets:new(?TABLE, [set, protected,
    named_table, {read_concurrency, true}]),
  {ok, #state{}}.


handle_call(Request, _From, State) ->
  lager:error("unexpected request: ~p", [Request]),
  {stop, {error, unexpected_request}, State}.

%%
% 第一次请求 2小时以后 删除token
% 以及每天零点，调整调用次数为0
%%
handle_cast({token_clear, TokenRecord}, State) ->
  erlang:send_after(get_today_over_second(),self(),{clear_interval,TokenRecord#ham_user_token{num = 0}}),
  Ref = erlang:send_after(clear_timer_interval(),self(),{clear_interval,TokenRecord#ham_user_token{token = <<>>}}),
  ets:insert(?TABLE,{TokenRecord#ham_user_token.user_id,Ref}),
  {noreply, State};

%%
% 第二次请求或多次请求
%%
handle_cast({token_more_clear, TokenRecord}, State) ->
  UserId = TokenRecord#ham_user_token.user_id,
  try ets:lookup_element(?TABLE, UserId, 2) of
    Pid ->
      erlang:cancel_timer(Pid),
      Ref = erlang:send_after(clear_timer_interval(),self(),{clear_interval,TokenRecord#ham_user_token{token = <<>>}}),
      ets:insert(?TABLE,{TokenRecord#ham_user_token.user_id,Ref})
  catch
    _:_ ->
      ""
  end,
  {noreply, State};

handle_cast(Msg, State) ->
  lager:error("unexpected msg: ~p", [Msg]),
  {noreply, State}.


handle_info({clear_interval, Value}, State) ->
  mnesia:dirty_write(ham_user_token,Value),
  {noreply, State};

handle_info(Info, State) ->
  lager:error("unexpected info: ~p", [Info]),
  {noreply, State}.

terminate(_Reason, _State) ->
  ok.

code_change(_OldVsn, State, _Extra) ->
  {ok, State}.

%%--------------------------------------------------------------------
%% Internal Functions
%%--------------------------------------------------------------------
clear_timer_interval() ->
  timer:seconds(7200).

get_today_over_second() ->
  Now = calendar:datetime_to_gregorian_seconds(calendar:local_time()) - calendar:datetime_to_gregorian_seconds({{1970,1,1}, {8,0,0}}),
  timer:seconds((Now-((Now+28800) rem 86400)) + 86400 - Now).  

